package com.netease.xone.fragment;

import java.lang.reflect.Type;
import java.util.List;

import protocol.meta.AppImage;
import android.os.Bundle;
import android.support.v4.view.PagerAdapter;
import android.support.v4.view.ViewPager;
import android.support.v4.view.ViewPager.OnPageChangeListener;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.LinearLayout;
import android.widget.ProgressBar;

import com.actionbarsherlock.view.Menu;
import com.actionbarsherlock.view.MenuInflater;
import com.actionbarsherlock.view.MenuItem;
import com.google.gson.Gson;
import com.google.gson.reflect.TypeToken;
import com.netease.pkgRelated.XoneConsts;
import com.netease.pkgRelated.XoneConsts.MENU_ID;
import com.netease.xone.R;
import com.netease.xone.itemview.RenderImageBrowser;


public class FragmentImageBrowser extends FragmentBase {

    public static final int TYPE_IMAGE = 0x1;
    public static final int TYPE_GALLERY = 0x2;
    
    private int mIndex = 0;
    private int mType = TYPE_IMAGE;
    
    private ViewPager mViewPager;
    private LinearLayout mProgressContainer;
    private ProgressBar mProgressBar;
    
    private List<AppImage> mList;
    
    public static FragmentImageBrowser newInstance(int index, int type, String value) {
        FragmentImageBrowser frag = new FragmentImageBrowser();
        Bundle b = new Bundle();
        b.putInt(XoneConsts.EXTRA_DURATION, index);
        b.putInt(XoneConsts.EXTRA_OPERATE, type);
        b.putString(XoneConsts.EXTRA_PATH, value);
        frag.setArguments(b);
        return frag;
    }

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setHasOptionsMenu(true);
        
        mIndex = getArguments().getInt(XoneConsts.EXTRA_DURATION);
        mType = getArguments().getInt(XoneConsts.EXTRA_OPERATE);
        String value = getArguments().getString(XoneConsts.EXTRA_PATH);
        
        Gson gson = new Gson();
        Type collectionType = new TypeToken<List<AppImage>>(){}.getType();
        mList = gson.fromJson(value, collectionType);
        
        // 设置title
        setTitle();
    }
    
    private void setTitle() {
        String value = null;
        
        if (mType == TYPE_IMAGE) {
            value = getString(R.string.popup_image_preview);
        } else {
            value = (mIndex+1) + "/" + mList.size();
        }
        
        getActivity().setTitle(value);
    }
    
    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
        View view;
        
        view = inflater.inflate(R.layout.fragment_image_browser, null, false);
        findViews(view);
        
        return view;
    }
    
    @Override
    public void onCreateOptionsMenu(Menu menu, MenuInflater inflater) {
        super.onCreateOptionsMenu(menu, inflater);
        
        MenuItem item = null;
        
        item = menu.add(Menu.NONE, MENU_ID.menu_info_discuss, 0, "download");
        item.setIcon(R.drawable.icon_action_down_selector);
        item.setShowAsAction(MenuItem.SHOW_AS_ACTION_IF_ROOM | MenuItem.SHOW_AS_ACTION_WITH_TEXT);
    }
    
    @Override
    public boolean onOptionsItemSelected(MenuItem item) {
        switch (item.getItemId()) {
        case MENU_ID.menu_info_discuss:
            return true;
        default:
            return super.onOptionsItemSelected(item);
        }
    }
    
    private void findViews(View root) {
        mViewPager = (ViewPager)root.findViewById(R.id.pager);
        mProgressContainer = (LinearLayout)root.findViewById(R.id.progress_container);
        mProgressBar = (ProgressBar)root.findViewById(R.id.progress);
        
        mProgressContainer.setVisibility(View.GONE);
        
        mViewPager.setAdapter(new ImageAdapter(mList));
        mViewPager.setCurrentItem(mIndex);
        
        mViewPager.setOnPageChangeListener(new OnPageChangeListener() {

            @Override
            public void onPageScrollStateChanged(int arg0) {
                
            }

            @Override
            public void onPageScrolled(int arg0, float arg1, int arg2) {
                
            }

            @Override
            public void onPageSelected(int arg0) {
                mIndex = arg0;
                setTitle();
            }
            
        });
    }
    
    
    public static class ImageAdapter extends PagerAdapter {
        private List<AppImage> mList;
        
        public ImageAdapter (List<AppImage> list) {
            mList = list;
        }
        
        @Override
        public Object instantiateItem(ViewGroup container, int position) {
            View root = null;
            
            root = View.inflate(container.getContext(), R.layout.item_view_image_browser, null);
            RenderImageBrowser render = new RenderImageBrowser(root);
            render.renderView(container.getContext(), mList.get(position));
            
            container.addView(root, 0);
            
            return root;
        }

        @Override
        public void destroyItem(ViewGroup container, int position, Object object) {
            container.removeView((View)object);
        }
        
        @Override
        public int getCount() {
            if (null!= mList) {
                return mList.size();
            }
            
            return 0;
        }

        @Override
        public boolean isViewFromObject(View arg0, Object arg1) {
            if (arg0 == arg1) {
                return true;
            }
            return false;
        }
        
    }
}
